package com.atguigu.cloud.user.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.atguigu.cloud.user.pojo.User;
import com.atguigu.cloud.user.prop.UserProperties;
import com.atguigu.cloud.user.service.UserService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.*;

import java.time.ZonedDateTime;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping(value = "/api/user")
@RefreshScope // 开启热更新
public class UserController {

    @Autowired
    private UserService userService ;

    @Value("${server.port}")
    private String port ;

    @Autowired
    private HttpServletRequest request ;

    @Autowired
    private UserProperties userProperties;

    // 热点配
    @SentinelResource("hot")
    @GetMapping(value = "/findUserByUserId/{userId}")
    public User findUserByUserId(@PathVariable(value = "userId") Long userId) {
        if(userId == 1) {
            try {
                TimeUnit.MILLISECONDS.sleep(60);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }else if(userId == 2){
            throw new RuntimeException("出现异常了");
        }
        return userService.findUserByUserId(userId);
    }

    @PostMapping(value = "/findUser")
    public User findUser(@RequestBody User user) {
        System.out.println(user);
        return null ;
    }

    // 测试流控模式之关联
    @GetMapping(value = "/updateUserById")
    public String updateUserById() {
        return "修改用户数据成功";
    }

    // 测试流控模式之链路
    @GetMapping(value = "/save")
    public String save() {
        userService.queryUsers();
        System.out.println("保存用户");
        return "订单保存成功" ;
    }

    @GetMapping(value = "/query")
    public String query() {
        userService.queryUsers();
        System.out.println("查询用户");
        return "查询用户成功" ;
    }
}